using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using emp.ProcessFlow;
using emp.Model.SysManage;
using System.Data.SqlClient;

public partial class crm_cells_ListsTypeAdd : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            int ltt_uid = 0;
            if (Request.Params["ltt_uid"] != null && Request.Params["ltt_uid"].Equals("") == false)
            {
                try
                {
                    ltt_uid = int.Parse(Request.Params["ltt_uid"]);
                    emp.Model.MListsType mListsType = new emp.BLL.ListsType().getListsTypeInfo(ltt_uid);
                    this.LTT_NAME.Text = mListsType.LTT_NAME;
                    this.LTT_DESC.Text = mListsType.LTT_DESC;
                }
                catch { }
            }
            LTT_UID.Value = ltt_uid.ToString();
        }
    }
    protected void save_Click(object sender, EventArgs e)
    {
        UserInfo myUserInfo = new UserInfo();
        if ((UserInfo)Session["USER_KEY"] != null)
        {
            myUserInfo = (UserInfo)Session["USER_KEY"];

        }

        int ltt_uid = int.Parse(LTT_UID.Value);
        emp.Model.MListsType mListsType = new emp.Model.MListsType();
        mListsType.LTT_UID = ltt_uid;
        mListsType.LTT_NAME = this.LTT_NAME.Text;
        mListsType.LTT_DESC = this.LTT_DESC.Text;

        if (ltt_uid == 0)
        {
            //ListsTypeController listTypeController = new ListsTypeController();
            //listTypeController.Insert(mListsType);

            string sql = "INSERT INTO LIST_TYPE (LTT_NAME,LTT_DESC,[LTT_CREATE_DATE],[LTT_CREATE_USER]) VALUES ('" + this.LTT_NAME.Text + "','" + this.LTT_DESC.Text + "',getdate(),'" + myUserInfo .UserID.ToString()+ "')";

            bool bo = ExecSql(sql);


            Response.Redirect("ListTypeList.aspx", true);
        }
        else
        {
            //ListsTypeController listTypeController = new ListsTypeController();
            //listTypeController.Update(mListsType);

            string sql = " update [LIST_TYPE] set [LTT_NAME]='" + this.LTT_NAME.Text + "',[LTT_DESC]='" + this.LTT_DESC.Text + "'";

            bool bo = ExecSql(sql);
            Response.Redirect("ListTypeList.aspx", true);
        }

    }


    private DataTable getTableInfo(string sql)
    {
        DataSet data = new DataSet();
        using (SqlDataAdapter sda = new SqlDataAdapter())
        {
            try
            {
                sda.SelectCommand = new SqlCommand();
                sda.SelectCommand.CommandText = sql;
                sda.SelectCommand.Connection = new SqlConnection(emp.SqlServerDAL.SQLHelper.CONN_STRING_NON_DTC);
                sda.Fill(data);
            }
            catch { }
        }
        return data.Tables[0];
    }


    private bool ExecSql(string sql)
    {
        bool ret = false;
        using (SqlConnection conn = new SqlConnection(emp.SqlServerDAL.SQLHelper.CONN_STRING_NON_DTC))
        {
            conn.Open();
            using (SqlTransaction trans = conn.BeginTransaction())
            {
                try
                {
                    emp.SqlServerDAL.SQLHelper.ExecuteNonQuery(trans, CommandType.Text, sql, null);
                    trans.Commit();
                    ret = true;
                }
                catch (SqlException err)
                {
                    trans.Rollback();
                }
            }
        }
        return ret;
    }

}
